package com.exercise.animations.flip_animator

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.exercise.animations.R
import com.exercise.animations.databinding.ActivityCardFlipBinding
import com.exercise.struct.base.BaseActivity
import com.exercise.struct.viewmodel.BaseViewModel

class CardFlipActivity : BaseActivity<BaseViewModel, ActivityCardFlipBinding>() {
    override val bindingInflater: (LayoutInflater) -> ActivityCardFlipBinding
        get() = ActivityCardFlipBinding::inflate
    override val modelClass: Class<BaseViewModel>
        get() = BaseViewModel::class.java

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setSupportActionBar(binding.toolbar)
        if (savedInstanceState == null) {
            supportFragmentManager.beginTransaction()
                .add(R.id.container, CardFrontFragment())
                .commit()
        }
    }

    override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
        val flipMenu = menu?.findItem(R.id.action_flip) ?: return super.onPrepareOptionsMenu(menu)
        if (showingBack) {
            flipMenu.setIcon(R.drawable.ic_text_format_24)
        } else {
            flipMenu.setIcon(R.drawable.ic_image_24)
        }
        return super.onPrepareOptionsMenu(menu)
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {

        menuInflater.inflate(R.menu.menu_flip, menu)
        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return when (item.itemId) {
            R.id.action_flip -> {
                flipCard()
                invalidateOptionsMenu()
                true
            }

            else -> super.onOptionsItemSelected(item)
        }
    }

    private var showingBack = false
    private fun flipCard() {
        if (showingBack) {
            supportFragmentManager.popBackStack()
            showingBack = false
            return
        }
        showingBack = true
        supportFragmentManager.beginTransaction()
            .setCustomAnimations(
                R.animator.card_flip_right_in,
                R.animator.card_flip_right_out,
                R.animator.card_flip_left_in,
                R.animator.card_flip_left_out
            )
            .replace(R.id.container, CardBackFragment())
            .addToBackStack(null)
            .commit()
    }

    override fun initData() {
    }

    override fun initView() {
    }

    class CardFrontFragment : Fragment() {
        override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            return inflater.inflate(R.layout.layout__card_front, container, false)
        }
    }

    class CardBackFragment : Fragment() {
        override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            return inflater.inflate(R.layout.layout__card_back, container, false)
        }
    }

    companion object {
        fun start(context: Context) {
            Intent(context, CardFlipActivity::class.java).also {
                context.startActivity(it)
            }
        }
    }
}
